package com.example.meituan.activity.man;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.widget.Toolbar;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;


import com.example.meituan.MainActivity;
import com.example.meituan.R;
import com.example.meituan.activity.user.RegisterUserActivity;
import com.example.meituan.dao.AdminDao;
import com.example.meituan.dao.JiaMi;
import com.example.meituan.until.FileImgUntil;


public class RegisterManActivity extends AppCompatActivity {
    private ActivityResultLauncher<String> getContentLauncher;

    private Uri url;
    //打开文件选择器
    private void openGallery(View v){
        getContentLauncher.launch("image/*");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_register_man);


        //实现一个返回的功能
        Toolbar toolbar=findViewById(R.id.register_man_toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                                                 @Override
                                                 public void onClick(View v) {
                                                     onBackPressed();//返回功能
                                                    //finish();

                                                 }
                                             }
        );
        //获取头像
        Drawable defaultDrawable= ContextCompat.getDrawable(this,R.drawable.photo);

        EditText idText=findViewById(R.id.register_man_id);
        EditText pwdText=findViewById(R.id.register_man_pwd);
        EditText pwdTryText=findViewById(R.id.register_man_pwd_try);
        EditText nameText=findViewById(R.id.register_man_name);
        EditText desText=findViewById(R.id.register_man_des);
        Spinner spinner = findViewById(R.id.register_man_type);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.shop_type_array));
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        ImageView img=findViewById(R.id.register_man_photo);
        TextView reg=findViewById(R.id.register_man_reg);


        getContentLauncher=registerForActivityResult(new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>() {
                    @Override
                    public void onActivityResult(Uri result) {
                        if(result!=null)
                        {
                            img.setImageURI(result);
                            url =result;
                        }else{
                            Toast.makeText(RegisterManActivity.this,"未选择头像",Toast.LENGTH_SHORT).show();
                        }
                    }
                }

        );

        img.setOnClickListener(new View.OnClickListener() {
                                   @Override
                                   public void onClick(View v) {
                                       //打开手机相册,实现上传图片
                                       openGallery(v);

                                   }
                               }

        );

        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isValid = true;
                Drawable drawable = img.getDrawable();
                String id = idText.getText().toString().trim();
                String pwd = pwdText.getText().toString().trim();
                String pwdTry = pwdTryText.getText().toString().trim();
                String name = nameText.getText().toString().trim();
                String des = desText.getText().toString().trim();

                // 验证图片是否为空
                if (drawable instanceof BitmapDrawable) {
                    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                    if (defaultDrawable != null && bitmap.sameAs(((BitmapDrawable) defaultDrawable).getBitmap())) {
                        Toast.makeText(RegisterManActivity.this, "图片不能为空", Toast.LENGTH_SHORT).show();
                        isValid = false;
                    }
                }

                // 验证其他字段
                if (id.isEmpty()) {
                    Toast.makeText(RegisterManActivity.this, "账号不能为空", Toast.LENGTH_SHORT).show();
                    isValid = false;
                }
                if (pwd.isEmpty()) {
                    Toast.makeText(RegisterManActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                    isValid = false;
                }
                if (pwdTry.isEmpty()) {
                    Toast.makeText(RegisterManActivity.this, "再次输入密码不能为空", Toast.LENGTH_SHORT).show();
                    isValid = false;
                }
                if (!pwd.equals(pwdTry)) {
                    Toast.makeText(RegisterManActivity.this, "两次输入密码不一致", Toast.LENGTH_SHORT).show();
                    isValid = false;
                }
                if (name.isEmpty()) {
                    Toast.makeText(RegisterManActivity.this, "请输入店铺名称", Toast.LENGTH_SHORT).show();
                    isValid = false;
                }
                if (des.isEmpty()) {
                    Toast.makeText(RegisterManActivity.this, "请输入店铺描述", Toast.LENGTH_SHORT).show();
                    isValid = false;
                }
                Object selectedItem = spinner.getSelectedItem();
                String selectedType = selectedItem.toString();
                if (selectedType.isEmpty()) {
                    Toast.makeText(RegisterManActivity.this, "店铺类型不能为空", Toast.LENGTH_SHORT).show();
                    isValid = false;
                }

                // 如果所有验证都通过，则进行注册操作
                if (isValid) {
                    pwd = JiaMi.encryptPassword(pwd);
                    String path = FileImgUntil.getImgPath();
                    FileImgUntil.saveImageBitmapToFile(url, RegisterManActivity.this, path);
                    int a = AdminDao.saveBusinessUser(id, pwd, name, des, selectedType, path);
                    if (a == 1) {
                        Toast.makeText(RegisterManActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent(RegisterManActivity.this, MainActivity.class);
                        startActivity(intent);
                    } else {
                        Toast.makeText(RegisterManActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });
        }



}